package com.fintecher.controller;

import com.alibaba.fastjson.JSON;
import com.fintecher.pojo.Dictionary;
import com.fintecher.pojo.Positive;
import com.fintecher.service.IDictionaryService;
import com.fintecher.service.IPositiveService;
import com.fintecher.utils.Utils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Controller
@RequestMapping("/positive")
public class PositiveController {
    @Resource
    private IPositiveService positiveService;
    @Resource
    private IDictionaryService dictionaryService;
    Utils utils = new Utils();

    @RequestMapping(value = "get_PositiveList")
    public String getPositiveList(HttpServletRequest request, Model model) {
        return "bill_management";
    }

    @RequestMapping("/toBM")
    public String toBM() {
        return "bill_market";
    }

    @RequestMapping("getType")
    public void getType(HttpServletRequest request) {

        //获取承兑人类型
        List<Dictionary> dictList = dictionaryService.selectPositiveType(5);
        request.setAttribute("dictList", dictList);

        //获取产品类型
        List<Dictionary> dictionaries = dictionaryService.selectPositiveType(1);
        request.setAttribute("dictionaries", dictionaries);
    }

    /**
     * @return
     * @throws ParseException
     * @throws IOException
     */
    @RequestMapping("getJSONBySell")
    public void getJSONBySell(HttpServletRequest request, HttpServletResponse response,
                              String acceType, String deadLine, String positiveStartMoney,
                              String positiveEndMoney, String inquiryPerson, String address,
                              String posiType, String bothState) throws ParseException, IOException {
        //获取票据信息
        Positive positive = new Positive();
        positive.setBothState(bothState);
        positive.setPosiType(posiType);
        positive.setAcceType(acceType);
        positive.setPositiveStartMoney(positiveStartMoney);
        positive.setPositiveEndMoney(positiveEndMoney);
        positive.setInquiryPerson(inquiryPerson);
        positive.setDeadLine(deadLine);
        positive.setPositiveAddress(address);
        List<Positive> positives = positiveService.selectAll(positive);

        //判定剩余期限
        for (int i = 0; i < positives.size(); i++) {
            Positive p = positives.get(i);
            if (p.getDeadLine().equals("1")) {
                p.setDeadLine("一月期");
            } else if (p.getDeadLine().equals("2")) {
                p.setDeadLine("二月期");
            } else if (p.getDeadLine().equals("3")) {
                p.setDeadLine("三月期");
            } else if (p.getDeadLine().equals("4")) {
                p.setDeadLine("四月期");
            } else if (p.getDeadLine().equals("5")) {
                p.setDeadLine("五月期");
            } else if (p.getDeadLine().equals("6")) {
                p.setDeadLine("六月期");
            } else {
                p.setDeadLine("-----");
            }
            p.setSurplus(utils.getTimeDifferenceForDay(p.getEndTime()));
        }
        String positivesJson = JSON.toJSONString(positives);
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(positivesJson);
        response.getWriter().flush();
        response.getWriter().close();
    }

    @RequestMapping("getSelectPositiveConditionBySell")
    public String getSelectPositiveConditionBySell(HttpServletRequest request) {
        //获取产品和承兑人类型
        getType(request);
        //实时更新已过期的票据信息
        positiveService.updatePositive();
        return "sell_market";
    }

    /**
     * @param request
     * @throws ParseException
     */
    @RequestMapping("getSelectPositiveConditionByBuy")
    public String getSelectPositiveConditionByBuy
    (HttpServletRequest request) throws ParseException {
        //获取产品和承兑人类型
        getType(request);
        //实时更新已过期的票据信息
        positiveService.updatePositive();
        return "buy_market";
    }


    @RequestMapping("getJSONForOne")
    public void getJSONForOne(HttpServletRequest request, HttpServletResponse response, String positiveNumber) throws IOException {
        Positive positive = positiveService.selectOne(positiveNumber);
        String positiveJson = JSON.toJSONString(positive);
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(positiveJson);
        response.getWriter().flush();
        response.getWriter().close();

    }
}
